package cn.tedu.charging.order.service;

import cn.tedu.charging.common.constant.MqttDestinationConst;
import cn.tedu.charging.common.pojo.message.ChargingCheckResultMessage;
import cn.tedu.charging.common.pojo.message.ChargingStartCheckMessage;
import cn.tedu.charging.common.pojo.message.DelayCheckMessage;
import cn.tedu.charging.common.pojo.param.OrderAddParam;
import cn.tedu.charging.common.utils.CronUtil;
import cn.tedu.charging.common.utils.SnowflakeIdGenerator;
import cn.tedu.charging.common.utils.XxlJobTaskUtil;
import cn.tedu.charging.order.amqp.AmqpProducer;
import cn.tedu.charging.order.feign.DeviceClient;
import cn.tedu.charging.order.feign.UserClient;
import cn.tedu.charging.order.mqtt.MqttTemplate;
import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
@Slf4j
public class OrderServiceImpl implements OrderService{
    @Autowired
    private DeviceClient deviceClient;
    @Autowired
    private UserClient userClient;
    @Autowired
    private MqttTemplate<ChargingStartCheckMessage> mqttTemplate;
    //生成 构造方法参数 数据中心编号 以及机器编号(0-31)
    private SnowflakeIdGenerator generator
            =new SnowflakeIdGenerator(1,1);
    @Autowired
    private AmqpProducer<DelayCheckMessage> amqpProducer;
    @Override
    public String createOrder(OrderAddParam param) {
        //1.检查枪状态
        checkGun(param.getGunId());
        //2.检查车主状态
        checkUser(param.getUserId());
        //3.生成订单编号
        String billId=generatorBillId();
        //4.给设备发送一条开始自检的命令
        startCheckCommand(param,billId);//orderNo billId 一样的
        //5.发送一个延迟消息
        sendCheckResultNoRes(param,billId);
        //6.创建调度中心定时任务,让订单定时检查状态
        //业务可以知道订单最大充电时长,延迟时间cron计算
        //XxlJobTaskUtil.createJobTask(CronUtil.delayCron(1000*60*2),2,billId);
        return billId;
    }

    private void sendCheckResultNoRes(OrderAddParam param,String billId) {
        //1.组织消息对象DelayCheckMessage orderNo userId  gunId pileId vehicleId
        DelayCheckMessage msg=new DelayCheckMessage();
        msg.setOrderNo(billId);
        msg.setUserId(param.getUserId());
        msg.setPileId(param.getPileId());
        msg.setGunId(param.getGunId());
        //2.发送延迟消息
        amqpProducer.sendDelay(msg);
    }

    private void startCheckCommand(OrderAddParam param, String billId) {
        //1.数据准备一个消息
        ChargingStartCheckMessage message=new ChargingStartCheckMessage();
        message.setOrderNo(billId);
        message.setUserId(param.getUserId());
        message.setGunId(param.getGunId());
        //2. charging/device/check/{pileId}
        String destination="charging/device/check/"+param.getPileId();
        mqttTemplate.sendDefault(destination,message);
        //MqttDestinationConst.START_GUN_CHECK_PREFIX+param.getPileId();
    }

    private String generatorBillId() {
        return generator.nextId()+"";
    }

    private void checkUser(Integer userId) {
        userClient.bindedVehicle(userId);
    }

    private void checkGun(Integer gunId) {
        deviceClient.checkGun(gunId);
    }
}
